package cn.edu.sdyu.ie.crs.service.impl;

import cn.edu.sdyu.ie.crs.common.response.LabelValue;
import cn.edu.sdyu.ie.crs.common.response.NamedValue;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.domain.Device;
import cn.edu.sdyu.ie.crs.repository.ClassroomRepository;
import cn.edu.sdyu.ie.crs.repository.DeviceRepository;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import cn.edu.sdyu.ie.crs.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceRepository deviceRepository;

    @Override
    public List<Device> findAll() {
        return deviceRepository.findAll();
    }

    @Override
    public Page<Device> findByPage(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC,"id"));
        return deviceRepository.findAll(pageable);
    }

    @Override
    public Device findDeviceById(Integer id) {
        Optional<Device> optional = deviceRepository.findById(id);
        if(optional.isPresent()) {
            return optional.get();
        } else {
            log.warn("指定设备信息[ID:{}]不存在", id);
            return null;
        }

        // return DeviceRepository.findById(id).orElse(null);
    }

    @Override
    public void saveDevice(Device device) {
        if(device.getId() == null) {
            device.setCreateTime(new Date());
        }
        deviceRepository.save(device);
    }

    @Override
    public void deleteDeviceById(Integer id) {
        deviceRepository.deleteById(id);
    }

    @Override
    public Long countDevice() { return deviceRepository.count();}

    @Override
    public Long countDeviceByRuntimeStatus(Integer runtimeStatus) {
        Device device = new Device();
        device.setRuntimeStatus(runtimeStatus);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("runtimeStatus", ExampleMatcher.GenericPropertyMatchers.exact());
        Example<Device> example = Example.of(device, matcher);
        return deviceRepository.count(example);
    }

    @Override
    public Device findDeviceByDeviceCode(String deviceCode) {
        return deviceRepository.findByDeviceCode(deviceCode);
    }

    @Override
    public List<NamedValue> countGroupByDeviceType() {
        List<LabelValue> count = deviceRepository.countGroupByDeviceType();
        List<NamedValue> list = new ArrayList<>();
        Map<String, String> label = new HashMap<>();
        label.put("1", "灯光");
        label.put("2", "插座");
        label.put("3", "USB");
        label.put("4", "环境检测设备");
        count.forEach(item -> {
            list.add(new NamedValue(label.get(item.getLabel()), item.getValue()));
        });

        return list;
    }

    @Override
    public List<Device> findDevicesByClassroomId(Integer classroomId) {
        return deviceRepository.findDevicesByClassroomId(classroomId);
    }

}
